package com.example.config

import com.baomidou.mybatisplus.core.MybatisConfiguration
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean
import org.apache.ibatis.session.SqlSessionFactory
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory
import org.mybatis.spring.annotation.MapperScan
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import javax.sql.DataSource


/**
 *
 *
 * @author wangli
 * @date 2025-10-10
 */
@Configuration
@MapperScan("com.example.repository.mapper")
class MyBatisConfiguration {

    @Bean
    fun sqlSessionFactory(datasource: DataSource): SqlSessionFactory {
        val configuration = MybatisConfiguration()
        configuration.isMapUnderscoreToCamelCase = true
        val factoryBean = MybatisSqlSessionFactoryBean()
        factoryBean.configuration = configuration
        factoryBean.setDataSource(datasource)
        factoryBean.setMapperLocations(*PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"))
        return factoryBean.`object` as DefaultSqlSessionFactory

    }
}
